/**
 *  Copyright (c) 2010 The edtf project.  All rights reserved.
 *
 *  This file is part of edtf (Event Driven Testing Framework).
 *
 *  edtf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  edtf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with edtf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/edtf.
 */

package org.xepf.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * 
 */
public class FileUtils {

    public static String readFile(String filename, int bufferCapacity) throws IOException {
        StringBuilder stringBuffer = new StringBuilder(bufferCapacity);
        BufferedReader reader = new BufferedReader(new FileReader(filename));

        int charactersRead = 0;
        int totalCharactersRead = 0;
        char[] characters = new char[bufferCapacity];

        while ((charactersRead = reader.read(characters)) > -1) {
            stringBuffer.append(String.valueOf(characters));
            totalCharactersRead += charactersRead;

            if (totalCharactersRead > bufferCapacity) {
                System.out.println("You can gain some speed by increasing the bufferCapacity..");
            }
        }

        reader.close();

        return stringBuffer.toString();
    }

    public static long getFileSize(String filename) throws Exception {

        File file = new File(filename);

        if ( file == null || !file.exists() || !file.isFile()) {
            throw new Exception("File '" + filename + "' does not exist or it is not a file. Running directory is '"+ System.getProperty("user.dir") +"'.");
        }
        
        return file.length();
    }
}
